package com.at.source3;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.time.Duration;

/**
 * 命令连接kafka，发送消息
 * bin/kafka-console-producer.sh --broker-list master:9092 --topic topic_1
 *
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/9/23 22:23
 */
public class KafkaSourceDemo3 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("master:9092,hadoop141:9092,hadoop-test:9092") // 指定卡发卡节点的地址和端口
                .setGroupId("hc") // 消费者组的id
                .setTopics("topic_1") // 消费topic
                .setValueOnlyDeserializer(new SimpleStringSchema()) // 指定反序列化器
                .setStartingOffsets(OffsetsInitializer.earliest()) // flink消费kafka的策略
                .build();
        env
//                .fromSource(kafkaSource, WatermarkStrategy.noWatermarks(), "kafkaSource")
                // 数据源中发送水位线 乱序 延迟3s
                .fromSource(kafkaSource, WatermarkStrategy.forBoundedOutOfOrderness(Duration.ofSeconds(3)), "kafkaSource")
                .print();
        env.execute();
    }
}
/**
 * Kafka消费者参数：
 *   auto.reset.offsets
 *      earliest: 如果有offset，从offset继续消费；如果没有offset，从 最早 开始消费
 *      latest：  如果有offset，从offset继续消费；如果没有offset，从 最新 开始消费
 *
 * flink的KafkaSource，offset消费策略：OffsetsInitializer，默认是 earliest
 *      earliest: 一定从 最早 开始消费
 *      latest：  一定从 最新 开始消费
 */